import Joi from 'joi'
import { Result } from '~~/lib/helper/result'
import prisma from '~~/lib/prisma'

export default defineEventHandler(async (event) => {
  const userId = getLoginId(event)
  const body = await getQuery<{ pageNum: number; pageSize: number }>(event)
  const schema = Joi.object({
    pageNum: Joi.number().required(),
    pageSize: Joi.number().required(),
  })

  try {
    await schema.validateAsync(body)
  } catch (error: any) {
    return Result.Error('参数错误！' + error.message)
  }
  try {
    const { pageNum, pageSize } = body
    // 创建文章
    const notes = await prisma.note.findMany({
      where: { uid: userId },
      // 页码
      skip: (+pageNum - 1) * +pageSize,
      // 返回数据
      take: +pageSize,
    })
    console.log('notes', notes)
    return Result.Ok(notes, 200, '获取文章成功！')
  } catch (error) {
    console.log('error', error)
    return Result.Error('获取文章失败！')
  }
})
